package controller

import (
	"context"
	"develop_admin/model"
	"develop_admin/proto/product"
	"develop_admin/utils"
	"fmt"
	"strconv"
)

type Product struct{}

func (p *Product) AddProduct(ctx context.Context, in *product.AddProductRequest, out *product.AddProductResponse) error {
	var product model.Product
	product.ProductName = in.Name
	product.ProductNum, _ = strconv.Atoi(in.Num)
	product.ProductAddress = in.Address
	product.ProductDesc = in.Desc
	atoi, _ := strconv.Atoi(in.Price)
	product.ProductPrice = float64(atoi)
	product.ProductPic = in.Pic

	result := utils.Db.Create(&product)

	if result.Error != nil { //产品添加失败
		out.Code = 500
		out.Msg = "产品添加失败"
		return result.Error
	}

	out.Code = 500
	out.Msg = "产品添加成功"
	return nil
}

func (p *Product) ShowProduct(ctx context.Context, in *product.ShowProductRequest, out *product.ShowProductResponse) error {
	var products []model.Product
	result := utils.Db.Find(&products)
	if result.Error != nil { //代表获取商品列表失败
		out.Code = 500
		out.Msg = "获取商品列表失败"
		return result.Error
	}
	var product_reps []*product.AddProductRequest
	for i := 0; i < len(products); i++ {
		var product_rep product.AddProductRequest
		product_rep.Name = products[i].ProductName
		product_rep.Price = fmt.Sprintf("%.2f", products[i].ProductPrice)
		product_rep.Desc = products[i].ProductDesc
		product_rep.Num = strconv.FormatInt(int64(products[i].ProductNum), 10)
		product_rep.Address = products[i].ProductAddress
		product_rep.Pic = products[i].ProductPic
		product_reps = append(product_reps, &product_rep)
	}
	out.Code = 200
	out.Msg = "获取商品列表成功"
	out.Products = product_reps
	return nil
}

func (p *Product) DeleteProduct(ctx context.Context, in *product.AddProductRequest, out *product.AddProductResponse) error {
	num, _ := strconv.Atoi(in.Num)

	float, _ := strconv.ParseFloat(in.Price, 10)

	product := model.Product{ProductName: in.Name, ProductPic: in.Pic, ProductDesc: in.Desc, ProductNum: num, ProductPrice: float, ProductAddress: in.Address}

	result := utils.Db.Limit(1).Where("product_name=?", in.Name).Where("product_address=?", in.Address).Where("product_num=?", num).Where("product_desc=?", in.Desc).Where("product_price=?", float).Where("product_pic=?", in.Pic).Delete(&product) //删除该商品 仅一件
	if result.Error != nil {                                                                                                                                                                                                                         //删除商品失败
		out.Code = 500
		out.Msg = "商品删除失败"
		return result.Error
	}

	out.Code = 200
	out.Msg = "商品删除成功"
	return nil
}

func (p *Product) SearchProduct(ctx context.Context, in *product.SearchProductRequest, out *product.ShowProductResponse) error {
	var products []model.Product

	result := utils.Db.Where( "product_name like ?", "%"+in.Name+"%").Find(&products)
	if result.Error != nil { //查询报错
		out.Code = 500
		out.Msg = "商品查询失败"
		return result.Error
	}
	var product_reps []*product.AddProductRequest
	for i := 0; i < len(products); i++ {
		var product_rep product.AddProductRequest
		product_rep.Name = products[i].ProductName
		product_rep.Price = fmt.Sprintf("%.2f", products[i].ProductPrice)
		product_rep.Desc = products[i].ProductDesc
		product_rep.Num = strconv.FormatInt(int64(products[i].ProductNum), 10)
		product_rep.Address = products[i].ProductAddress
		product_rep.Pic = products[i].ProductPic
		product_reps = append(product_reps, &product_rep)
	}
	out.Code = 200
	out.Msg = "商品查询成功"
	out.Products=product_reps

	return nil
}
